package com.naja.event.tracking.extract.config;

import com.naja.event.tracking.extract.observer.Base2bizExtractObserver;
import com.naja.event.tracking.extract.observer.MutuallyExclusiveBase2bizExtractObserver;
import com.naja.event.tracking.extract.observer.SimpleBase2bizExtractObserver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @auther wangjianying
 * @date 2023/09/19 10:02
 */
@Configuration
public class EventBase2BizExtractConfig  {
    private final static Logger logger = LoggerFactory.getLogger(EventBase2BizExtractConfig.class);

    private EventBase2BizExtractProperties eventBase2BizExtractProperties;
    public EventBase2BizExtractConfig(EventBase2BizExtractProperties eventBase2BizExtractProperties) {
        this.eventBase2BizExtractProperties = eventBase2BizExtractProperties;
    }

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public Map<String, Base2bizExtractObserver> getBase2bizExtractObserverMap() {
        Map<String, EventBase2BizExtractObserverProperties> observer = this.eventBase2BizExtractProperties.getObserver();
        HashMap<String, Base2bizExtractObserver> resultMap = new HashMap<>();
        if (observer!=null) {
            for (Map.Entry<String, EventBase2BizExtractObserverProperties> entry : observer.entrySet()) {
                String key = entry.getKey();
                EventBase2BizExtractObserverProperties value = entry.getValue();
                if (CollectionUtils.isEmpty(value.getSortedTagEvents())) {
                    logger.warn("事件观察者{}未配置事件标签,跳过注册", key);
                    continue;
                }
                if (!StringUtils.hasText(value.getEventName())) {
                    logger.warn("事件观察者{}未配置事件名,跳过注册", key);
                    continue;
                }
                ArrayList<String> reverseSortedTagEvents = new ArrayList<>(value.getSortedTagEvents());
                Collections.reverse(reverseSortedTagEvents);
                SimpleBase2bizExtractObserver simpleBase2BizExtractObserver = new SimpleBase2bizExtractObserver(reverseSortedTagEvents, value.getEventName(), value.getEventDesc());
                if (key.contains("_")) {
                    String parentKey = key.substring(0, key.indexOf("_"));
                    if (!resultMap.containsKey(parentKey)) {
                        resultMap.put(parentKey, new MutuallyExclusiveBase2bizExtractObserver());
                    }
                    ((MutuallyExclusiveBase2bizExtractObserver) resultMap.get(parentKey)).addBase2bizExtractObserver(simpleBase2BizExtractObserver);
                } else {
                    resultMap.put(key, simpleBase2BizExtractObserver);
                }
            }
        }
        return resultMap;
    }

}
